//
//  DigitAlbumViewController.m
//  NewApp
//
//  Created by  on 12-6-29.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DigitAlbumViewController.h"
#import "DigitAlbumDetailsViewController.h"
#import "Entity.h"

@interface DigitAlbumViewController ()

@end

@implementation DigitAlbumViewController

@synthesize tableView,datasourceObjects;

#pragma mark -
#pragma mark - Views lifecycle and memory management

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //Build table view and data source
    UITableView *tbv = [[UITableView alloc] initWithFrame:CGRectMake(0.f, 25.f, 320.f, 342.f) style:UITableViewStylePlain];
    tbv.backgroundColor = [UIColor clearColor];
    tbv.delegate = self;
    tbv.dataSource = self;
    tbv.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView = tbv;
    [tbv release];
    
    [self.view addSubview:self.tableView];
    
    NSMutableArray *array = [[NSMutableArray alloc] init];
    self.datasourceObjects = array;
    [array release];
    
	//Send request
    [self.engine digitAlbumPageInfo];
    self.title = @"返回";
}

- (void) dealloc {
    [tableView release];
    [datasourceObjects release];
    [super dealloc];
}

#pragma mark -
#pragma mark - Override methods

- (void) requestDidFinished:(RequestEngine *)reqEngine withObject:(id)object {
    
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dict = (NSDictionary *) object;
        [dict retain];
        
        NSString *title = [dict objectForKey:kTopTitle];
        if (title != nil && title.length > 0)
            self.titleLabel.text = title;
        
        if ([dict objectForKey:kData] != nil) {
            NSArray *array = [dict objectForKey:kData];
            for (NSDictionary *d in array) {
                Entity *entity = [[Entity alloc] initWithDictionary:d];
                [self.datasourceObjects addObject:entity];
            }
        }
        
        [self.tableView reloadData];
        
        [dict release];
        
    }
}

#pragma mark -
#pragma mark - UITableView delegate

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    Entity *selectedEntity = [self.datasourceObjects objectAtIndex:indexPath.row];
    if (selectedEntity != nil) {
        DigitAlbumDetailsViewController *viewCtrl = [[DigitAlbumDetailsViewController alloc] initWithAlbum:selectedEntity];
        [self.navigationController pushViewController:viewCtrl animated:YES];
        [viewCtrl release];
    }
    
}

#pragma mark -
#pragma mark - UITableView datasource

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datasourceObjects.count;
}

- (UITableViewCell *) tableView:(UITableView *)tableView 
          cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *digitAlbumCellIdentifier = @"DigitAlbumCellIdentifier";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:digitAlbumCellIdentifier];
    
    if (nil == cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:digitAlbumCellIdentifier] autorelease];
    }
    
    UIView *v = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
    v.backgroundColor = (indexPath.row % 2 == 0) ? [UIColor colorWithRed:81.f/255.f green:81.f/255.f blue:81.f/255.f alpha:1.f] : [UIColor colorWithRed:51.f/255.f green:51.f/255.f blue:51.f/255.f alpha:1.f];
    cell.backgroundView = v;
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    Entity *e = [self.datasourceObjects objectAtIndex:indexPath.row];
    cell.textLabel.text = e.title;
    
    cell.accessoryType = (indexPath.row == 6) ? UITableViewCellAccessoryNone : UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}




@end 
